<template>
    <div>
        <div>
            <el-input size="small"
                      v-model="jobLevel.name"
                      style="width: 300px"
                      prefix-icon="el-icon-plus"
                      placeholder="添加职称..."></el-input>
            <el-select v-model="jobLevel.titleLevel" placeholder="职称等级" size="small"
                       style="margin-left: 8px;margin-right: 8px">
                <el-option
                        v-for="item in titleLevels"
                        :key="item"
                        :label="item"
                        :value="item">
                </el-option>
            </el-select>
            <el-button icon="el-icon-plus" size="small" type="primary" @click="addJobLevel">添加</el-button>
        </div>
        <div style="margin-top: 10px">
            <el-table
                    :data="jobLevels"
                    border
                    stripe
                    @selection-change="handleSelectionChange"
                    size="small"
                    style="width: 80%">
                <el-table-column
                        type="selection"
                        width="55">
                </el-table-column>
                <el-table-column
                        prop="id"
                        label="编号"
                        width="55">
                </el-table-column>
                <el-table-column
                        prop="name"
                        label="职称名称"
                        width="150">
                </el-table-column>
                <el-table-column
                        prop="titleLevel"
                        label="职称等级"
                        width="150">
                </el-table-column>
                <el-table-column
                        label="是否启用"
                        width="80">
                    <template slot-scope="scope">
                        <el-tag type="success" v-if="scope.row.enabled">已启用</el-tag>
                        <el-tag type="info" v-else>未启用</el-tag>
                    </template>
                </el-table-column>
                <el-table-column
                        prop="createDate"
                        label="创建时间"
                        width="150">
                </el-table-column>
                <el-table-column label="操作">
                    <!--                    slot-scope是一个占位符-->
                    <template slot-scope="scope">
                        <!--                        scope.$index表示当前操作的行数,scope.row表示当前行对应的Json-->
                        <el-button
                                size="mini"
                                @click="showEdit(scope.$index, scope.row)">编辑
                        </el-button>
                        <el-button
                                size="mini"
                                type="danger"
                                @click="handleDelete(scope.$index, scope.row)">删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <el-button type="danger" @click="batchDelete" size="small" style="margin-top: 8px"
                       :disabled="multipleSelection.length==0">批量删除
            </el-button>
        </div>
        <el-dialog
                title="修改职称"
                :visible.sync="dialogVisible"
                width="30%">
            <div>
                <table>
                    <tr>
                        <td><el-tag>职称名</el-tag></td>
                        <td><el-input size="small" v-model="updateJob.name"></el-input></td>
                    </tr>
                    <tr>
                        <td><el-tag>职称等级</el-tag></td>
                        <el-select v-model="updateJob.titleLevel" placeholder="职称等级" size="small"
                                   style="margin-left: 8px;margin-right: 8px">
                            <el-option
                                    v-for="item in titleLevels"
                                    :key="item"
                                    :label="item"
                                    :value="item">
                            </el-option>
                        </el-select>
                    </tr>
                    <tr>
                        <td><el-tag>是否启用</el-tag></td>
                        <td>
                            <el-switch
                                    v-model="updateJob.enabled"
                                    active-text="启用"
                                    inactive-text="禁用"
                                    active-color="#13ce66"
                                    inactive-color="#ff4949">
                            </el-switch>
                        </td>
                    </tr>
                </table>
            </div>
            <span slot="footer" class="dialog-footer">
                <el-button size="small" @click="dialogVisible = false">取 消</el-button>
                <el-button size="small" type="primary" @click="doUpdate">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
    import {Message} from "element-ui";

    export default {
        name: "JobLevelManager",
        // 钩子方法，在页面加载el的时候调用后台接口请求数据,vue2之后官方推荐在这个钩子方法进行数据初始化
        mounted() {
            this.GetJobLevel();
        },
        data(){
            return{
                jobLevel:{
                    name:'',
                    titleLevel:''
                },
                titleLevels: [
                    '正高级',
                    '副高级',
                    '中级',
                    '初级',
                    '员级'
                ],
                jobLevels:[],
                dialogVisible:false,
                updateJob:{
                    name:'',
                    titleLevel:'',
                    enabled:false
                },
                multipleSelection:[]
            }
        },
        methods:{
            GetJobLevel(){
                this.getRequest("/system/basic/job/").then(resp=>{
                    if(resp){
                        this.jobLevels = resp;
                    }
                })
            },
            //添加职称的时候，需要判断用户是否有输入值，如果没输入需要提示输入
            addJobLevel(){
                if(this.jobLevel.name && this.jobLevel.titleLevel){
                    this.postRequest("/system/basic/job/", this.jobLevel).then(resp => {
                        if (resp) {
                            this.GetJobLevel();//刷新表格
                            this.jobLevel = {
                                name: '',
                                titleLevel: ''
                            };
                        }
                    })
                }else {
                    Message.error({message: '请填写完整再添加职称信息！'});
                }
            },
            showEdit(index, data){
                Object.assign(this.updateJob,data);//对象拷贝，第一个参数为拷贝到的对象地址，第二个参数为被拷贝的对象，即从B拷贝内容到A
                this.dialogVisible = true;
            },
            doUpdate(){
                this.putRequest("/system/basic/job/",this.updateJob).then(resp=>{
                    if(resp){
                        this.GetJobLevel();
                        this.updateJob = {
                            name:'',
                            titleLevel:'',
                            enabled:false
                        };
                        this.dialogVisible = false;
                    }
                })
            },
            handleDelete(index, data){
                this.$confirm('此操作将永久删除【' + data.name + '】职称, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.deleteRequest("/system/basic/job/" + data.id).then(resp => {
                        if (resp) {
                            this.GetJobLevel();
                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            // 当选择多选框时，把多选框行内的值赋值给multipleSelection对象
            handleSelectionChange(val){
                this.multipleSelection = val;
            },
            batchDelete(){
                this.$confirm('此操作将永久删除【'+this.multipleSelection.length+'】条数据, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    let ids= '?';
                    //拼接id数组
                    this.multipleSelection.forEach(item=>{
                        ids += 'ids=' + item.id + '&';
                    })
                    this.deleteRequest("/system/basic/job/batchDelete"+ids).then(resp => {
                        if (resp) {
                            this.GetJobLevel();
                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            }

        }
    }
</script>

<style>
    body .el-table th.gutter {
        display: table-cell !important
    }
</style>